package com.kc.business.service.impl;

import com.kc.business.domain.BizRoleMenu;
import com.kc.business.mapper.BizRoleMenuMapper;
import com.kc.business.service.IBizRoleMenuService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * FileName: BizRoleMenuServiceImpl
 * Author:   TP
 * Date:     2020-10-30 20:40
 * Description:
 */
@Service
public class BizRoleMenuServiceImpl implements IBizRoleMenuService {

    @Autowired
    private BizRoleMenuMapper roleMenuMapper;

    @Override
    public List<BizRoleMenu> select(BizRoleMenu bizRoleMenu) {
        return roleMenuMapper.select(bizRoleMenu);
    }

    @Override
    public int deleteByRoleId(Integer id) {
        if (id != null && id > 0) {
            return roleMenuMapper.deleteByRoleId(id);
        }
        return 0;
    }

    @Override
    public int saveBatch(int roleId, List<Integer> menuIds, String username) {
        if (roleId > 0) {
            List<BizRoleMenu> toSaveRoleMenus = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(menuIds)) {
                for (Integer menuId : menuIds) {
                    BizRoleMenu roleMenu = new BizRoleMenu();
                    roleMenu.setRoleId(roleId);
                    roleMenu.setMenuId(menuId);
                    roleMenu.setCreateTime(new Date());
                    roleMenu.setCreateBy(username);
                    toSaveRoleMenus.add(roleMenu);
                }
                // 新增角色菜单资源信息
                return roleMenuMapper.insertList(toSaveRoleMenus);
            }
            return 1;
        }
        return 0;
    }
}
